package com.dream.base.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author: WangKunHui
 * Date: 2017/8/18 14:13
 * Desc:
 * E-mail:life_artist@163.com
 */
public class ThreadPoolManager {

    private static ThreadPoolManager instance;

    /**
     * 任务队列的线程池
     */
    private ExecutorService taskExecutor;

    private ThreadPoolManager() {
    }

    public static ThreadPoolManager getInstance() {
        if (instance == null) {
            instance = new ThreadPoolManager();
        }

        return instance;
    }

    /**
     * 获取任务队列的线程池
     *
     * @return
     */
    public ExecutorService getTaskExecutor() {
        if (taskExecutor == null || taskExecutor.isShutdown() || taskExecutor.isTerminated()) {
            taskExecutor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
        }

        return taskExecutor;
    }

}
